﻿using WebSoftSeo.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebSoftSeo.Models;
using System.Data;

namespace WebSoftSeo.Areas.Admin.Controllers
{
    public class ConsultController : Controller
    {
        private WebSoftSeoEntities db = new WebSoftSeoEntities();
        //
        // GET: /Admin/Consult/

        public ActionResult Index()
        {
            int pageNo = 0;
            foreach (var key in Request.Form.AllKeys)
            {
                if (key.StartsWith("PageNo:"))
                {
                    pageNo = int.Parse(key.Substring(7));
                    break;
                }
            }

            PagerInfo pi = PagerInfo.Get("Consult", 10);
            pi.RowCount = db.Consults.Where(m=>m.ParentID==null).OrderByDescending(p => p.ID).Count();
            pi.Navigate(pageNo);
            int StartRow = pi.PageNo * pi.PageSize;
            var _consult = db.Consults.Where(m=>m.ParentID==null).OrderByDescending(a => a.ID).Skip(StartRow).Take(pi.PageSize);
            return View(_consult);
        }
        [HttpPost, ActionName("Index")]
        public ActionResult Index1()
        {
            int pageNo = 0;
            foreach (var key in Request.Form.AllKeys)
            {
                if (key.StartsWith("PageNo:"))
                {
                    pageNo = int.Parse(key.Substring(7));
                    break;
                }
            }
            PagerInfo pi = PagerInfo.Get("Consult", 10);
            pi.RowCount = db.Consults.Where(m => m.ParentID == null).OrderByDescending(p => p.ID).Count();
            pi.Navigate(pageNo);
            int StartRow = pi.PageNo * pi.PageSize;
            var _consult = db.Consults.Where(m => m.ParentID == null).OrderByDescending(a => a.ID).Skip(StartRow).Take(pi.PageSize);
            return PartialView("IndexAjax", _consult);
        }
        //
        // GET: /Comment/Reply
        public ActionResult Reply(int id = 0)
        {
            Consult consult = db.Consults.Find(id);
            if (consult == null)
            {
                return HttpNotFound();
            }
            ViewBag.ID = new SelectList(db.Consults, "ID");
            return View(consult);
        }
        //POST: /Comment/Reply
        [HttpPost, ActionName("Reply")]
        public ActionResult Reply(Consult consult, int? id = 0)
        {
            Consult consu = db.Consults.Find(id);
            if (ModelState.IsValid)
            {
                string username = User.Identity.Name;
                Membership member = db.Memberships.Where(m => m.Username == username).FirstOrDefault();
                //set value get reply contents
                string _value = Request.Form["comments"];
                if (_value == "")
                {
                    ViewBag.error = "Chưa nhập nội dung trả lời";
                    if (consu == null)
                    {
                        return HttpNotFound();
                    }
                    ViewBag.ID = new SelectList(db.Consults, "ID");
                    return View(consu);
                }
                else
                {
                    consu.Name = member.Username;
                    consu.Email = member.Email;
                    consu.Contents = _value;
                    consu.Date = DateTime.Now;
                    consu.Status = true;
                    consu.ParentID = consult.ID;
                    consu.IPuser = System.Web.HttpContext.Current.Request.UserHostAddress;
                    db.Consults.Add(consu);
                    db.SaveChanges();
                }
                return RedirectToAction("Index", "Consult");
            }
            return View(consu);
        }


        // Post Status
        [HttpPost]
        public ActionResult Edit(int? id)
        {
            Consult consult = db.Consults.Where(m => m.ID == id).FirstOrDefault();
            consult.Status = true;
            db.Entry(consult).State = EntityState.Modified;
            db.SaveChanges();
            string results = "Approved";
            return Json(results);
        }

        // Post Reply
        [HttpPost]
        public ActionResult EditReply(int? id)
        {
            return Json(id);
        }
        //
        // GET: /Consult/Delete

        public ActionResult Delete(int id = 0)
        {
            string username = User.Identity.Name;
            Membership member = db.Memberships.Where(m => m.Username == username).FirstOrDefault();
            Consult consult = db.Consults.Find(id);

            if (consult == null)
            {
                return HttpNotFound();
            }
            if (consult.ParentID == null)
            {
                ViewBag.email = consult.Email;
                ViewBag.username = consult.Name;
            }
            else
            {
                    ViewBag.email = member.Email;
                    ViewBag.username = member.Username;
                List<Consult> listCommentParentId = db.Consults.Where(c => c.ID == consult.ID).ToList();
            }
            return View(consult);
        }

        //
        // POST: /Comment/Consult

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Consult consult = db.Consults.Find(id);
            string username = User.Identity.Name;
            Membership member = db.Memberships.Where(m => m.Username == username).FirstOrDefault();
            Consult consu = db.Consults.Where(c => c.ParentID == consult.ID).FirstOrDefault();
            if (consult.ParentID != null ||  consult.ParentID == null)
            {
                if (consu != null)
                {
                    db.Consults.Remove(consult);
                    List<Consult> listCommentParentId = db.Consults.Where(c => c.ParentID == consult.ID).ToList();
                    foreach (var lst in listCommentParentId)
                    {
                        db.Consults.Remove(lst);
                    }
                }
                else
                {
                    db.Consults.Remove(consult);
                }
            }
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
